package cc.mrbird.febs.book.service.impl;

import cc.mrbird.febs.book.dto.HistoryDTO;
import cc.mrbird.febs.book.entity.Books;
import cc.mrbird.febs.book.entity.Ratings;
import cc.mrbird.febs.book.mapper.BooksMapper;
import cc.mrbird.febs.book.mapper.RatingsMapper;
import cc.mrbird.febs.book.service.IBooksService;
import cc.mrbird.febs.book.vo.HistoryVO;
import cc.mrbird.febs.book.vo.RecommendVO;
import cc.mrbird.febs.book.vo.UserBookList;
import cc.mrbird.febs.common.utils.Result;
import cc.mrbird.febs.common.utils.RowsData;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.awt.print.Book;
import java.util.List;
import java.util.regex.Pattern;

/**
 * @author wbt
 */
@Service("bookService")
public class BooksServiceImpl extends ServiceImpl<BooksMapper, Books> implements IBooksService {
    @Autowired
    private BooksMapper booksMapper;
    @Autowired
    private RatingsMapper ratingsMapper;


    //用户图书列表
    public Result<RowsData<Books>> userBootList(UserBookList vo) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(vo.getPublisher())) {
            queryWrapper.like("Publisher", vo.getPublisher());
        }
        if (!StringUtils.isEmpty(vo.getBookAuthor())) {
            queryWrapper.like("Book_Author", vo.getBookAuthor());
        }
        if (!StringUtils.isEmpty(vo.getBookTitle())) {
            queryWrapper.like("Book_Title", vo.getBookTitle());
        }
        Page<Object> page = PageHelper.startPage(vo.getCurPage(), vo.getPageSize());
        List<Books> list = this.baseMapper.selectList(queryWrapper);
        return Result.success(new RowsData<Books>(list, Math.toIntExact(page.getTotal()), page.getPages()), "操作成功");
    }

    public Books selectBookInfo(String bookId) {
        Books books = booksMapper.selectBookInfo(bookId);
        int sum = booksMapper.selectSum(bookId);
        int count = booksMapper.selectScore(bookId);
        if (count>0){
            books.setScore(String.valueOf(sum/count));
        }
        else {
            books.setScore(String.valueOf(sum/1));
        }
        return books;

    }

    public Result updateScore(String score, String bookId) {
        int i = booksMapper.updateScore(score, bookId);
        if (i>0){
            return Result.success("操作成功");
        }else {
            return Result.fail("操作失败");
        }

    }

    public Result<RowsData<HistoryDTO>> historyList(HistoryVO vo) {
        Page<Object> page = PageHelper.startPage(vo.getCurPage(), vo.getPageSize());
        List<HistoryDTO> historyList = booksMapper.historyList(vo);
        return Result.success(new RowsData<HistoryDTO>(historyList, Math.toIntExact(page.getTotal()), page.getPages()), "操作成功");
    }

    public Result getList() {
        List<Books> list = booksMapper.getList();
        return Result.success(list,"查询成功");
    }

    public Result<RowsData<HistoryDTO>> getRecommendList(HistoryVO vo) {
        Page<Object> page = PageHelper.startPage(vo.getCurPage(), vo.getPageSize());
        List<HistoryDTO> list = booksMapper.getRecommendList(vo);
        return Result.success(new RowsData<HistoryDTO>(list, Math.toIntExact(page.getTotal()), page.getPages()), "操作成功");
    }

    public Result<RowsData<HistoryDTO>> getBookListByAuthor(RecommendVO vo) {
        Page<Object> page = PageHelper.startPage(vo.getCurPage(), vo.getPageSize());
        List<HistoryDTO> list = booksMapper.getBookListByAuthor(vo);
        return Result.success(new RowsData<HistoryDTO>(list, Math.toIntExact(page.getTotal()), page.getPages()), "操作成功");
    }

    public boolean updateBook(Books books) {
        return booksMapper.updateBook(books);
    }
}
